home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0411.ZIP / PRINT.C < prev    next >
Text File  |  1985-09-23  |  13KB  |  428 lines

  1. /******************************************************************************/
  2. /*                                          */
  3. /* Program to print a file                              */
  4. /*                                          */
  5. /******************************************************************************/
  6. #include "stdio.h"
  7. main(argc,argv)
  8.     int argc;
  9.     char *argv[];
  10.     {
  11.     char outname[64],
  12.      headstr[241],
  13.      pagestr[4],
  14.      monstr[37],
  15.      prntdate[10],
  16.      sysdate[9];
  17.     int  outfile,
  18.      infile,
  19.      infilen,
  20.      i,
  21.      j,
  22.      c,
  23.      cntr,
  24.      length,
  25.      width,
  26.      left,
  27.      right,
  28.      top,
  29.      bottom,
  30.      condense,
  31.      heading,
  32.      rhspace,
  33.      lhspace,
  34.      datelen,
  35.      pageno,
  36.      pgnolen,
  37.      prntchrz,
  38.      prntlinz,
  39.      dataarea,
  40.      finished,
  41.      offset;
  42.     long value;
  43.     /**************************************************************************/
  44.     /*                                          */
  45.     /* If information requested, display syntax                   */
  46.     /*                                          */
  47.     /**************************************************************************/
  48.     if ((argc == 1) || (argv[1][0] == '?'))
  49.     {
  50.     puts("\n");
  51.     puts("PRINT <printfile> [/C] [/F<outfile>] [/W<nnn>] [/P<nnn>]\n");
  52.     puts("      [/T<nnn>] [/B<nnn>] [/L<nnn>] [/R<nnn>] [/H]\n\n");
  53.     puts("where   printfile  filespec for the file to be printed.\n");
  54.     puts("                   Default: None\n\n");
  55.     puts("        /C         condensed print mode.\n");
  56.     puts("                   Default: mode at time of invocation.\n\n");
  57.     puts("        /Foutfile  filespec for the output file when\n");
  58.     puts("                   output to file is desired.\n");
  59.     puts("                   Default: PRN\n\n");
  60.     puts("        /Wnnn      page width.\n");
  61.     puts("                   Default: 132\n");
  62.     puts("                   Minimum:   2+L+R\n");
  63.     puts("                   Maximum: 240\n\n");
  64.     puts("        /Pnnn      page length.\n");
  65.     puts("                   Default: 66\n");
  66.     puts("                   Minimum:   2+T+B\n");
  67.     puts("                   Maximum: 999\n");
  68.     puts("\n");
  69.     puts("Press any key to continue ... ");
  70.     c = '\0';
  71.     while (c == '\0')
  72.         {
  73.         c = ci();
  74.         }
  75.     for(cntr=0;cntr<0x1e;cntr++)
  76.         {
  77.         co('\b');
  78.         }
  79.     puts("        /Tnnn      no of blank lines at top of page.\n");
  80.     puts("                   Default:   5\n");
  81.     puts("                   Minimum:   0\n");
  82.     puts("                   Maximum:   P-B-2\n\n");
  83.     puts("        /Bnnn      no of blank lines at bottom of page.\n");
  84.     puts("                   Default:   5\n");
  85.     puts("                   Minimum:   0\n");
  86.     puts("                   Maximum:   P-T-2\n\n");
  87.     puts("        /Lnnn      no of characters for left margin.\n");
  88.     puts("                   Default:   5\n");
  89.     puts("                   Minimum:   0\n");
  90.     puts("                   Maximum:   W-R-1\n\n");
  91.     puts("        /Rnnn      no of characters for right margin.\n");
  92.     puts("                   Default:   0\n");
  93.     puts("                   Minimum:   0\n");
  94.     puts("                   Maximum:   W-L-1\n\n");
  95.     puts("        /H         don't print heading.\n");
  96.     puts("                   Default: print heading.\n");
  97.     exit(0);
  98.     }
  99.     /**************************************************************************/
  100.     /*                                          */
  101.     /* Display version information if requested                   */
  102.     /*                                          */
  103.     /**************************************************************************/
  104.     if (argv[1][0] == '#')
  105.     {
  106.     puts("\n");
  107.     puts("Program: PRINT\n");
  108.     puts("Author : Peter Townsend\n");
  109.     puts("Date   : 21Sep85\n");
  110.     puts("Time   : 15:08\n");
  111.     puts("Version: 1.0\n");
  112.     exit(0);
  113.     }
  114.     /**************************************************************************/
  115.     /*                                          */
  116.     /* Initialise                                  */
  117.     /*                                          */
  118.     /**************************************************************************/
  119.     top = 5;
  120.     bottom = 5;
  121.     left = 5;
  122.     right = 0;
  123.     width = 132;
  124.     length = 66;
  125.     outfile = -1;
  126.     condense = 0;
  127.     heading = 0;
  128.     headstr[0] = '\0';
  129.     prntchrz = 0;
  130.     pageno = 0;
  131.     /**************************************************************************/
  132.     /*                                          */
  133.     /* Check for wildcard characters in filename                  */
  134.     /*                                          */
  135.     /**************************************************************************/
  136.     for(cntr=0;cntr<strlen(argv[1]);cntr++)
  137.     {
  138.     if ((argv[1][cntr] == '*') || (argv[1][cntr] == '?'))
  139.         {
  140.         puts("\n");
  141.         puts("Sorry ... wildcards not catered for.\n");
  142.         exit(0);
  143.         }
  144.     }
  145.     /**************************************************************************/
  146.     /*                                          */
  147.     /* Attempt to open file to be printed                      */
  148.     /*                                          */
  149.     /**************************************************************************/
  150.     if ((infile = open(argv[1],0)) == -1)
  151.     {
  152.     printf("\nCannot access file %s\n",argv[1]);
  153.     exit(1);
  154.     }
  155.     fclose(infile);
  156.     dissect(value,i,j,bottom,condense,heading,left,length,right,width,argv,outname,argc,outfile,top);
  157.     /**************************************************************************/
  158.     /*                                          */
  159.     /* Check validity of page parameters                      */
  160.     /*                                          */
  161.     /**************************************************************************/
  162.     if ((top + bottom + heading) >= length)
  163.     {
  164.     puts("\n");
  165.     printf("TOP      = %3d\n",top);
  166.     printf("BOTTOM   = %3d\n",bottom);
  167.     printf("HEADING  = %3d\n",heading);
  168.     puts("--------------\n");
  169.     printf("LENGTH   = %3d\n",length);
  170.     puts("\n");
  171.     puts("You have not left any lines to allow printing of data.\n");
  172.     exit(0);
  173.     }
  174.     if ((right + left) >= width)
  175.     {
  176.     puts("\n");
  177.     printf("LEFT     = %3d\n",left);
  178.     printf("RIGHT    = %3d\n",right);
  179.     puts("--------------\n");
  180.     printf("WIDTH    = %3d\n",width);
  181.     puts("\n");
  182.     puts("You have not left any positions to print the data.\n");
  183.     exit(0);
  184.     }
  185.     if (width > 0xf0)
  186.     {
  187.     puts("\n");
  188.     puts("Maximum value for width = 240\n");
  189.     exit(0);
  190.     }
  191.     if (length > 0x3e7)
  192.     {
  193.     puts("\n");
  194.     puts("Maximum value for length = 999\n");
  195.     exit(0);
  196.     }
  197.     /**************************************************************************/
  198.     /*                                          */
  199.     /* Open output file                               */
  200.     /*                                          */
  201.     /**************************************************************************/
  202.     if (outfile == -1)
  203.     {
  204.     if ((outfile = open("prn",1)) == -1)
  205.         {
  206.         if ((outfile = creat("prn")) == -1)
  207.         {
  208.         puts("\n");
  209.         puts("Cannot access printer\n");
  210.         exit(1);
  211.         }
  212.         }
  213.     }
  214.     else
  215.     {
  216.     if ((outfile = open(outname,1)) == -1)
  217.         {
  218.         if ((outfile = creat(outname)) == -1)
  219.         {
  220.         puts("\n");
  221.         printf("Cannot access file %s\n",outname);
  222.         exit(1);
  223.         }
  224.         }
  225.     }
  226.     /**************************************************************************/
  227.     /*                                          */
  228.     /* If necessary, create heading string                      */
  229.     /*                                          */
  230.     /**************************************************************************/
  231.     if (heading)
  232.     {
  233.     /**********************************************************************/
  234.     /*                                      */
  235.     /* Create heading date                              */
  236.     /*                                      */
  237.     /**********************************************************************/
  238.     strcpy(sysdate,dates());
  239.     prntdate[0] = sysdate[3];
  240.     prntdate[1] = sysdate[4];
  241.     prntdate[2] = 0x20;
  242.     strcpy(monstr,"JanFebMarAprMayJunJulAugSepOctNovDec");
  243.     offset = (((sysdate[0] - '0') * 10) + (sysdate[1] - '0'));
  244.     offset = ((offset -1) * 3);
  245.     for(i = 3; i < 6; i++)
  246.         {
  247.         prntdate[i] = monstr[i+offset];
  248.         }
  249.     prntdate[6] = 0x20;
  250.     prntdate[7] = sysdate[6];
  251.     prntdate[8] = sysdate[7];
  252.     prntdate[9] = '\0';
  253.     /**********************************************************************/
  254.     /*                                      */
  255.     /* Calculate spacing either side of file name                  */
  256.     /*                                      */
  257.     /**********************************************************************/
  258.     infilen = strlen(argv[1]);
  259.     datelen = 0x9;
  260.     pgnolen = 0x9;
  261.     if ((infilen + datelen + pgnolen + 2) >= width)
  262.         {
  263.         lhspace = width - (ceil(infilen/2) + datelen);
  264.         rhspace = width - (ceil(infilen/2) + pgnolen);
  265.         if ((ceil(infilen/2) *2) != infilen)
  266.         {
  267.         rhspace = rhspace + 1;
  268.         }
  269.         }
  270.     else
  271.         {
  272.         lhspace = 1;
  273.         rhspace = 1;
  274.         }
  275.     /**********************************************************************/
  276.     /*                                      */
  277.     /* Initialise heading string                          */
  278.     /*                                      */
  279.     /**********************************************************************/
  280.     sprintf(headstr,"%s%s%s%sPage: ",prntdate,lhspace,argv[1],rhspace);
  281.     /**********************************************************************/
  282.     /*                                      */
  283.     /* Check for room to print heading and some data              */
  284.     /*                                      */
  285.     /**********************************************************************/
  286.     dataarea = width - (left + right);
  287.     dataarea = dataarea * (length - (top + bottom));
  288.     if ((strlen(headstr) + 3) > dataarea)
  289.         {
  290.         puts("\n");
  291.         puts("Insufficient space to print heading and data\n");
  292.         puts("within given parameters.\n");
  293.         exit(1);
  294.         }
  295.     }
  296.     /**************************************************************************/
  297.     /*                                          */
  298.     /* Attempt to open file to be printed                      */
  299.     /*                                          */
  300.     /**************************************************************************/
  301.     if ((infile = open(argv[1],0)) == -1)
  302.     {
  303.     printf("\nCannot access file %s\n",argv[1]);
  304.     exit(1);
  305.     }
  306.     /**************************************************************************/
  307.     /*                                          */
  308.     /* Print top margin lines if any                          */
  309.     /*                                          */
  310.     /**************************************************************************/
  311.     for(i = 0; i < top; i++)
  312.     {
  313.     putc('\n',outfile);
  314.     }
  315.     prntlinz = length + 1;
  316.     /**************************************************************************/
  317.     /*                                          */
  318.     /* Set condensed mode if desired                          */
  319.     /*                                          */
  320.     /**************************************************************************/
  321.     finished = ((c = fgetc(infile)) == EOF);
  322.     if (!finished)
  323.     {
  324.     if (condense)
  325.         {
  326.         putc(0x15,outfile);
  327.         }
  328.     }
  329.     /**************************************************************************/
  330.     /*                                          */
  331.     /* Print                                      */
  332.     /*                                          */
  333.     /**************************************************************************/
  334.     puts("\n");
  335.     while (!finished)
  336.     {
  337.     /**********************************************************************/
  338.     /*                                      */
  339.     /* Check for end of line operations                      */
  340.     /*                                      */
  341.     /**********************************************************************/
  342.     if ((prntchrz >= width) || (c == 0xa))
  343.         {
  344.         if (prntchrz >= width)
  345.         {
  346.         putc(0xd,outfile);
  347.         putc(0xa,outfile);
  348.         }
  349.         for(i = 0; i < left; i++)
  350.         {
  351.         putc(' ',outfile);
  352.         }
  353.         prntchrz = left;
  354.         prntlinz++;
  355.         }
  356.     /**********************************************************************/
  357.     /*                                      */
  358.     /* Check for end of page operations                      */
  359.     /*                                      */
  360.     /**********************************************************************/
  361.     if (prntlinz >= length)
  362.         {
  363.         /******************************************************************/
  364.         /*                                      */
  365.         /* If necessary, print heading                      */
  366.         /*                                      */
  367.         /******************************************************************/
  368.         if (heading)
  369.         {
  370.         pageno++;
  371.         sprintf(pagestr,"%3d",pageno);
  372.         fprintf(outfile,"%s%s\n",headstr,pagestr);
  373.         }
  374.         /******************************************************************/
  375.         /*                                      */
  376.         /* Print bottom, top, and left margins                  */
  377.         /*                                      */
  378.         /******************************************************************/
  379.         for(i = 0; i < (top + bottom); i++)
  380.         {
  381.         putc('\n',outfile);
  382.         }
  383.         for(i = 0; i < left; i++)
  384.         {
  385.         putc(' ',outfile);
  386.         }
  387.         prntchrz = left;
  388.         prntlinz = top;
  389.         }
  390.     /**********************************************************************/
  391.     /*                                      */
  392.     /* Print the character and get another                      */
  393.     /*                                      */
  394.     /**********************************************************************/
  395.     putc(c,outfile);
  396.     if ((c == 0xd) || (c == 0xa))
  397.         {
  398.         if (c == 0xa)
  399.         {
  400.         prntchrz = 0;
  401.         }
  402.         }
  403.     else
  404.         {
  405.         if (c == 0x9)
  406.         {
  407.         prntchrz = prntchrz + 3;
  408.         }
  409.         prntchrz++;
  410.         }
  411.     finished = ((c = fgetc(infile)) == EOF);
  412.     }
  413.     /**************************************************************************/
  414.     /*                                          */
  415.     /* Put a formfeed in the output device                      */
  416.     /*                                          */
  417.     /**************************************************************************/
  418.     putc(0xc,outfile);
  419.     /**************************************************************************/
  420.     /*                                          */
  421.     /* Tidy-up, close files                              */
  422.     /*                                          */
  423.     /**************************************************************************/
  424.     fclose(infile);
  425.     fclose(outfile);
  426.     exit(0);
  427.     }
  428.